package org.abettor.nookinfo.novel;

import java.util.List;

import org.abettor.nookinfo.R;
import org.abettor.nookinfo.service.NovelDownloadService;
import org.abettor.nookinfo.service.NovelDownloadService.NovelDownloadBinder;
import org.abettor.nookinfo.service.NovelDownloadService.NovelDownloadTask;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

/**
 * 小说下载管理器界面
 * @author shawn
 *
 */
public class NovelDownloadManager extends Activity
{
	private static int HANDLE_MSG_DELAY = 1000;
	private static int HANDLE_MSG_UPDATE = 1024;
	private NotificationManager notiMgm;
	private NovelDownloadService service;
	private ServiceConnection serviceConnection = new ServiceConnection()
	{
		@Override
		public void onServiceConnected(ComponentName name, IBinder iBinder)
		{
			NovelDownloadBinder binder = (NovelDownloadBinder) iBinder;
			service = binder.getService();
			List<NovelDownloadTask> tasks = service.getTasks();
			notiMgm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
			for(NovelDownloadTask t : tasks)
			{
				switch(t.status)
				{
				case NovelDownloadService.DOWNLOAD_STATUS_CANCEL:
				case NovelDownloadService.DOWNLOAD_STATUS_DONE:
				case NovelDownloadService.DOWNLOAD_STATUS_ERROR:
					notiMgm.cancel(NovelDownloadService.NOTIFICATION_TAG, t.id);
					break;
				}
			}
		}

		@Override
		public void onServiceDisconnected(ComponentName name)
		{
		}
	};
	private Handler handler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			if(msg.what == HANDLE_MSG_UPDATE)
			{
				LinearLayout layout = (LinearLayout) findViewById(R.id.novelDownloadLayout);
				layout.removeAllViews();
				ScrollView scroll = (ScrollView) findViewById(R.id.novelDownloadScroll);
				scroll.scrollTo(0, 0);

				List<NovelDownloadTask> tasks = service.getTasks();
				for(NovelDownloadTask task : tasks)
				{
					//	将任务信息依次显示到界面上
					LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
					params.gravity = 0x03 | 0x10;
					LinearLayout hori = new LinearLayout(NovelDownloadManager.this);
					hori.setLayoutParams(params);
					hori.setOrientation(LinearLayout.HORIZONTAL);
					layout.addView(hori);

					//	名字
					LayoutParams paramsName = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0F);
					paramsName.gravity = 0x11;
					TextView name = new TextView(NovelDownloadManager.this);
					name.setLayoutParams(paramsName);
					name.setText(task.name);
					name.setTextSize(14);
					name.setTextColor(0xFF000000);
					hori.addView(name);

					//	完成百分比
					LayoutParams paramsRate = new LayoutParams(50, LayoutParams.WRAP_CONTENT);
					paramsRate.gravity = 0x11;
					TextView rate = new TextView(NovelDownloadManager.this);
					rate.setLayoutParams(paramsRate);
					rate.setText(task.complete + "%");
					rate.setTextSize(14);
					rate.setTextColor(0xFF000000);
					hori.addView(rate);

					//	状态
					LayoutParams paramsStatus = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
					paramsStatus.gravity = 0x11;
					TextView status = new TextView(NovelDownloadManager.this);
					status.setLayoutParams(paramsStatus);
					status.setTextSize(14);
					status.setTextColor(0xFF000000);
					switch (task.status)
					{
					case NovelDownloadService.DOWNLOAD_STATUS_BEGIN:
						status.setText(R.string.novel_download_status_begin);
						break;
					case NovelDownloadService.DOWNLOAD_STATUS_DONE:
						status.setText(R.string.novel_download_status_done);
						break;
					case NovelDownloadService.DOWNLOAD_STATUS_ERROR:
						status.setText(R.string.novel_download_status_error);
						break;
					case NovelDownloadService.DOWNLOAD_STATUS_CANCEL:
						status.setText(R.string.novel_download_status_cancel);
						break;
					default:
						status.setText(R.string.novel_download_status_unknown);
						break;
					}
					hori.addView(status);

					//	按钮
					LayoutParams paramsButton = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
					paramsButton.gravity = 0x11;
					Button button = new Button(NovelDownloadManager.this);
					button.setLayoutParams(paramsButton);
					hori.addView(button);
					final NovelDownloadTask theTask = task;
					switch (task.status)
					{
					case NovelDownloadService.DOWNLOAD_STATUS_BEGIN:
						{
							//	取消下载
							button.setBackgroundResource(R.drawable.novel_download_working);
							button.setText(R.string.novel_download_manager_btn_cancel);
							button.setOnClickListener(new OnClickListener()
							{
								@Override
								public void onClick(View v)
								{
									service.cancel(theTask.id);
								}
							});
						}
						break;
					case NovelDownloadService.DOWNLOAD_STATUS_DONE:
						{
							//	开启页面浏览本地小说
							button.setBackgroundResource(R.drawable.novel_download_done);
							button.setText(R.string.novel_download_manager_btn_read);
							button.setOnClickListener(new OnClickListener()
							{
								@Override
								public void onClick(View v)
								{
									Intent novelIntent = new Intent();
									novelIntent.setClass(NovelDownloadManager.this, NovelLocalChapter.class);
									novelIntent.putExtra(NovelLocalChapter.EXTRA_NOVEL_ID, theTask.id);
									novelIntent.putExtra(NovelLocalChapter.EXTRA_NOVEL_NAME, theTask.name);
									startActivity(novelIntent);
								}
							});
						}
						break;
					case NovelDownloadService.DOWNLOAD_STATUS_ERROR:
						{
							//	重新开始下载
							button.setBackgroundResource(R.drawable.novel_download_error);
							button.setText(R.string.novel_download_manager_btn_redo);
							button.setOnClickListener(new OnClickListener()
							{
								@Override
								public void onClick(View v)
								{
									service.download(theTask.id, theTask.name);
								}
							});
						}
						break;
					case NovelDownloadService.DOWNLOAD_STATUS_CANCEL:
						{
							//	重新开始下载
							button.setBackgroundResource(R.drawable.novel_download_cancel);
							button.setText(R.string.novel_download_manager_btn_redo);
							button.setOnClickListener(new OnClickListener()
							{
								@Override
								public void onClick(View v)
								{
									service.download(theTask.id, theTask.name);
								}
							});
						}
						break;
					}

					//	分割线
					LayoutParams paramsSeperator = new LayoutParams(LayoutParams.FILL_PARENT, 3);
					TextView seperator = new TextView(NovelDownloadManager.this);
					seperator.setBackgroundColor(0x88888888);
					seperator.setLayoutParams(paramsSeperator);
					layout.addView(seperator);
				}

				postDelayed(runnable, HANDLE_MSG_DELAY);
			}

			super.handleMessage(msg);
		}
		
	};
	private Runnable runnable = new Runnable()
	{
		@Override
		public void run()
		{
			//	直接通知界面更新
			Message msg = new Message();
			msg.what = HANDLE_MSG_UPDATE;
			handler.sendMessage(msg);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.novel_download_manager);
		Intent servIntent = new Intent();
		servIntent.setClass(this, NovelDownloadService.class);
		bindService(servIntent, serviceConnection, BIND_AUTO_CREATE);

		//	不断的定时启动线程，显示下载情况
		handler.postDelayed(runnable, HANDLE_MSG_DELAY);
	}

	@Override
	public void onDestroy()
	{
		unbindService(serviceConnection);
		super.onDestroy();
	}
}
